package com.platform.minio;

import java.io.InputStream;
import java.util.concurrent.TimeUnit;

import javax.annotation.Resource;

import org.springframework.stereotype.Repository;

import io.minio.BucketExistsArgs;
import io.minio.GetObjectArgs;
import io.minio.GetPresignedObjectUrlArgs;
import io.minio.MakeBucketArgs;
import io.minio.MinioClient;
import io.minio.PutObjectArgs;
import io.minio.RemoveObjectArgs;
import io.minio.errors.MinioException;
import io.minio.http.Method;

/**
 * @ClassName:  Minio   
 * @Description:minio文件持久层 
 * @author: <a href="mailto:15909910367@163.com">吴先生</a>  
 * @date:   2020年9月30日 下午7:09:54   
 *     
 * @Copyright: 2020 吴元森.
 */
@Repository
public class Minio {

	@Resource
	private MinioClient minioClient;

	
	public String preview(String bucket,String fileName) {
		try {
			return minioClient.getPresignedObjectUrl(
			        GetPresignedObjectUrlArgs.builder()
			        .method(Method.GET)
			        .bucket(bucket)
			        .expiry((int)TimeUnit.DAYS.toSeconds(1))//有效期一天
			        .object(fileName)
			        .build());
		} catch (Exception e) {
			e.printStackTrace();
			return "";
		}
	}
	
	/**
	 * @Title: put   
	 * @Description: 插入对象
	 * @param bucket	节点名称
	 * @param fileName	文件名称
	 * @param stream	文件流
	 * @param size		文件大小
	 * @param contentType	文件头信息
	 * @throws MinioException      
	 * void
	 */
	public void put(String bucket, String fileName, InputStream stream, long size, String contentType)
			throws MinioException {
		try {
//			minioClient.putObject(bucket, fileName, stream, size, contentType);
			PutObjectArgs args = PutObjectArgs.builder()
					.bucket(bucket)
					.object(fileName)
					.stream(stream, size, -1)
					.contentType(contentType)
					.build();
			minioClient.putObject(args);
		} catch (Exception e) {
			throw new MinioException("上传文件失败， 失败原因：" + e.getMessage());
		}
	}

	/**
	 * @Title: get   
	 * @Description: 根据节点名称，文件名称获取文件
	 * @param bucketName	节点名称
	 * @param fileName	文件名称
	 * @return
	 * @throws MinioException      
	 * InputStream
	 */
	public InputStream get(String bucketName, String fileName) throws MinioException {
		try {
//			return minioClient.getObject(bucketName, fileName);
			GetObjectArgs args = GetObjectArgs.builder().bucket(bucketName)
					.object(fileName).build();
			return minioClient.getObject(args);
		} catch (Exception e) {
			throw new MinioException("获取文件失败， 失败原因：" + e.getMessage());
		}
	}

	/**
	 * @Title: delete   
	 * @Description: 删除文件服务中的文件
	 * @param bucketName	节点名称
	 * @param fileName		文件名称
	 * @throws MinioException      
	 * void
	 */
	public void delete(String bucketName, String fileName) throws MinioException {
		try {
//			minioClient.removeObject(bucketName, fileName);
			RemoveObjectArgs args = RemoveObjectArgs.builder()
					.bucket(bucketName)
					.object(fileName)
					.build();
			minioClient.removeObject(args);
		} catch (Exception e) {
			throw new MinioException("删除文件失败， 失败原因：" + e.getMessage());
		}
	}

	/**
	 * @Title: checkBucket   
	 * @Description: 检查存储节点是否存在
	 * @param bucket	节点名称
	 * @return
	 * @throws MinioException      
	 * boolean
	 */
	public boolean checkBucket(String bucket) throws MinioException {
		try {
//			return minioClient.bucketExists(bucket);
			return minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucket).build());
		} catch (Exception e) {
			throw new MinioException("检查bucket失败， 失败原因：" + e.getMessage());
		}
	}
	
	/**
	 * @Title: createBucket   
	 * @Description: 创建存储节点 
	 * @param bucket
	 * @throws MinioException      
	 * void
	 */
	public void createBucket(String bucket) throws MinioException{
		try {
//			minioClient.makeBucket(bucket);
			minioClient.makeBucket(MakeBucketArgs.builder().bucket(bucket).build());
		} catch (Exception e) {
			throw new MinioException("创建bucket：["+bucket+"]失败， 失败原因：" + e.getMessage());
		} 
	}

}
